<?php

namespace app\admin\controller;

use app\common\controller\Admin;
use think\Request;
use think\facade\Cache;

//商家表
class Business extends Admin
{
    public function index()
    {
    	$where = [];
    	$where[] = ['fwgl_id','=',session('login')['fwgl_id']];
        if($this->request->isPost())
        {
            $post = post();
            if(isset($post['name'])) $where[] = ['name','=',$post['name']];
            if(isset($post['status'])) $where[] = ['status','=',$post['status']];
            $list = ysh_select($this->request->controller(),['where'=>$where]);
        }else{
            $list = ysh_select($this->request->controller(),['where'=>$where]);
        }
        $this->assign('list',$list['list']['data']);
        //page
        $this->assign('page',$list['page']);
        //总个数
        $this->assign('count',$list['list']['total']);
        //显示条数
        $this->assign('limit',$list['list']['per_page']);
        //当前页数
        $this->assign('curl',$list['list']['current_page']);
        //页码数
        $this->assign('groups',$list['list']['last_page']);
        return $this->fetch();
    }
    
    public function indexs()
    {
        $id = get()['id'];
        $this->assign('id',$id);
        $where = [];
        $where['where'][] = ['business_id','=',$id];
        if($this->request->isPost())
        {
            $post = post();
            if(isset($post['batterybaid'])) $where['where'][] = ['batterybaid','like','%' . $post['batterybaid'] . '%'];
            $list = ysh_select('batteryba',$where);
        }else{
            $list = ysh_select('batteryba',$where);
        }
        $this->assign('list',$list['list']['data']);
        //page
        $this->assign('page',$list['page']);
        //总个数
        $this->assign('count',$list['list']['total']);
        //显示条数
        $this->assign('limit',$list['list']['per_page']);
        //当前页数
        $this->assign('curl',$list['list']['current_page']);
        //页码数
        $this->assign('groups',$list['list']['last_page']);
        return $this->fetch();
    }
    
    public function add()
    {
    	if($this->request->isPost())
    	{
    		$post = post();
    		if(Cache::get('addbusin'.session('login')['id']))
    		{
    			$post['pid'] = Cache::get('addbusin'.session('login')['id']);
    			Cache::rm('addbusin'.session('login')['id']);
    		}
    		$user = [];
    		$user['name'] = $post['nikename'];
    		$user['username'] = $post['username'];
    		$user['password'] = jm($post['password']);
    		$user['phone'] = $post['phone'];
    		$user['status'] = 10;
    		$user['qxurl'] = '39,40,41,42,43,53';
    		unset($post['phone']);
    		unset($post['password']);
    		unset($post['username']);
    		unset($post['nikename']);
    		$post['num'] = 5;
    		ysh_save('user',$user);
    		$userid = model('user')->id;
    		$post['user_id'] = $userid;
    		$model = ysh_save('business',$post);
    		if($model){
    			$this->redirect('index');
    		}
    		
    	}
    	return $this->fetch();
    }
    
    public function addss()
    {
        if($this->request->isPost())
        {
            $post = post();
            if(!isset($post['username'])) return false;
            $list = ysh_select('user',['where'=>
                [['username','=',$post['username']]]
            ],2);
            return $list;
        }
    }
    
    public function addfp()
    {
        if($this->request->isPost())
        {
            $post = post();
            Cache::set('addbusin'.session('login')['id'],$post['sid'],60);
            return true;
        }
    }
    
    public function ss()
    {
        if($this->request->isPost())
        {
            $post = post();
            if(!isset($post['batterybaid'])) return false;
            $list = ysh_select('batteryba',['where'=>
                [['batterybaid','=',$post['batterybaid']]]
            ],2);
            return $list;
        }
    }
    
    public function fp()
    {
        if($this->request->isPost())
        {
            $post = post();
            ysh_upload('batteryba',['id'=>$post['sid'],'business_id'=>$post['id']]);
            return true;
        }
    }
    
    public function upload()
    {
    	$id = input('id');
    	if($this->request->isPost())
    	{
    		$post = post();
    		if(isset($post['id']))unset($post['id']);
    		model('business')->save($post,['id'=>$id]);
    		return $this->redirect('index');
    	}
    	$list = model('business')->get($id)->toArray();
    	$this->assign('list',$list);
    	return $this->fetch();
    }
    
    public function assest(){
        $get = input('get.');
        //条数
        $limit = $get['limit']?? 10;
        //当前页数
        $curl = $get['page']?? 1;
        $name = $this->request->controller() . $this->request->action();
        $where = [];
        $pid = 'pids,user.business';
        $where[] = ['user_id','=',session('login')['id']];
        $order = 'id desc';
        if($this->request->isPost())
        {
            $post = input('post.');
            //订单编号
            $where['orderid'] = $post['orderid']??'';
            //开始结束时间
            $where['time'] = $post['time']??'';
            //状态
            $where['class'] = $post['class']??'';
            $wheres =[];
            if($where['time']){
                list($first,$last) = explode('至',$where['time']);
                $wheres[] = ['create_time','>',trim($first)];
                $wheres[] = ['overtime','<',trim($last)];
            }
            foreach ($where as $k=>$v){
                if(strlen($v)>0){
                    if($k !== 'time') $wheres[] = [$k,'=',$v];
                }
            }
            if($wheres){
                session($name,$wheres);
                $list = model('wallet')->order($order)->with($pid)->where($wheres)->paginate($limit);
                $page = $list->render();
                $list = $list->toArray()['data'];
                $count = count(model('wallet')->where($wheres)->select()->toArray());
            }else{
                Session::delete($name);
                $list = model('wallet')->order($order)->with($pid)->paginate($limit);
                $page = $list->render();
                $list = $list->toArray()['data'];
                $count = count(model('wallet')->select()->toArray());
            }
        }else{
            if(session($name)){
                $list = model('wallet')->order($order)->where(session($name))->with($pid)->paginate($limit);
                $page = $list->render();
                $list = $list->toArray()['data'];
                $count = count(model('wallet')->where(session($name))->select()->toArray());
            }else{
                $list = model('wallet')->order($order)->where($where)->with($pid)->paginate($limit);
                $page = $list->render();
                $list = $list->toArray()['data'];
                $count = count(model('wallet')->where($where)->select()->toArray());
            }
        }
        $this->assign('list',$list);
        $this->assign('page',$page);
        //总个数
        $this->assign('count',$count);
        //显示条数
        $this->assign('limit',$limit);
        //当前页数
        $this->assign('curl',$curl);
        //页码数
        $groups = ceil($count/$limit);
        $this->assign('groups',$groups);
        //资产总额
        //进项
        $jmoney = model('wallet')->field('money')->where([['class','=',7],['user_id','=',session('login')['id']]])->select()->toArray();
        $money = 0;
        foreach ($jmoney as $v){
            $money = $v['money']*1+$money;
        }
        //出项
        $jmoney = model('wallet')->field('money')->where([['class','=',12],['user_id','=',session('login')['id']]])->select()->toArray();
        $moneys = 0;
        foreach ($jmoney as $v){
            $moneys = $v['money']*1+$moneys;
        }
        //平台总收益
        $money = $money-$moneys;
        $this->assign('ptsy',$money);
        return $this->fetch();
    }
    public function viewupload()
    {
    	$id = input('id');
    	if($this->request->isPost())
    	{
    		$post = post();
    	}
    	$list = model('batteryba')->where('id','=',$id)->with('user,userpid,fwgl,business')->find()->toArray();
    	// pr($list);
    	$this->assign('list',$list);
    	return $this->fetch();
    }
     //分配设备管理员
    public function upsstp()
    {
        if($this->request->isPost())
        {
            $post = post();
            if(!isset($post['username'])) return false;
            $list = ysh_select('user',['where'=>
                [['username','=',$post['username']]]
            ],2);
        	$list['code'] = 200;
            return $list;
        }
    }
    public function upfptp()
    {
        if($this->request->isPost())
        {
            $post = post();
            //选中的设备管理人
            $list = ysh_select('user',['where'=>[['id','=',$post['sid']]]],2);
            //当前机台
            $now = ysh_select('batteryba',['where'=>['id'=>$post['id']]],2);
            //当前机台是否有绑定设备管理员
            if($now['list']['pid'] > 0){
            	//有设备管理员重新绑定
            	//判断旧管理员还有其他机台管理不
		        $userall = ysh_select('batteryba',['limit'=>0,'where'=>['pid'=>$now['list']['pid']]]);
		        $userall = array_filter($userall);
		        if(!$userall){
		        	//没有其他机台管理，取消该账号设备管理员身份
		    		$model = ysh_upload('user',['id'=>$now['list']['pid'],'sbgl'=>2]);
		        }
		        //机台绑定新的设备管理员
		        $model = ysh_upload('batteryba',['id'=>$now['list']['id'],'pid'=>$post['sid']]);
		        //给该账号设备管理员身份
		        $model = ysh_upload('user',['id'=>$list['list']['id'],'sbgl'=>1]);
            }else{
            	//新增设备管理员
		        //把设备管理人绑定到机台下
		        $model = ysh_upload('batteryba',['id'=>$now['list']['id'],'pid'=>$post['sid']]);
		        $model = ysh_upload('user',['id'=>$list['list']['id'],'sbgl'=>1]);
            }
            return $model;
        }
    }
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
}
